home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Tools / Turbo Pascal V7 / DOCDEMO.ZIP / STREAM1.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-11-03  |  7.2 KB  |  281 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Vision 2.0 Demo                        }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. { Create and display a collection of graphical objects:
  9.   Points, Circles, Rectangles. Then put them on a stream
  10.   to be read by another program (STREAM2.PAS).
  11.  
  12.   If you are running this program in the IDE, be sure to
  13.   enable the full graphics save option when you load TURBO.EXE:
  14.  
  15.     turbo -g
  16.  
  17.   This ensures that the IDE fully swaps video RAM and keeps
  18.   "dustclouds" from appearing on the user screen when in
  19.   graphics mode. You can enable this option permanently
  20.   via the Options|Environment|Startup dialog.
  21.  
  22.   This program uses the Graph unit and its .BGI driver files to
  23.   display graphics on your system. The "PathToDrivers"
  24.   constant defined below is set to \TP\BGI, which is the default
  25.   location of the BGI files as installed by the INSTALL program.
  26.   If you have installed these files in a different location, make
  27.   sure the .BGI file for your system (EGAVGA.BGI, etc.) is in the
  28.   current directory or modify the "PathToDrivers" constant
  29.   accordingly.
  30. }
  31.  
  32. program Stream1;
  33.  
  34. uses
  35.   Objects, Graph;
  36.  
  37. const
  38.   PathToDrivers = '\TP\BGI';  { Default location of *.BGI files }
  39.  
  40. { ********************************** }
  41. { ******  Graphical Objects  ******* }
  42. { ********************************** }
  43.  
  44. type
  45.   PGraphObject = ^TGraphObject;
  46.   TGraphObject = object(TObject)
  47.     X,Y: Integer;
  48.     constructor Init;
  49.     procedure Draw; virtual;
  50.     procedure Store(var S: TStream); virtual;
  51.   end;
  52.  
  53.   PGraphPoint = ^TGraphPoint;
  54.   TGraphPoint = object(TGraphObject)
  55.     procedure Draw; virtual;
  56.   end;
  57.  
  58.   PGraphCircle = ^TGraphCircle;
  59.   TGraphCircle = object(TGraphObject)
  60.     Radius: Integer;
  61.     constructor Init;
  62.     procedure Draw; virtual;
  63.     procedure Store(var S: TStream); virtual;
  64.   end;
  65.  
  66.   PGraphRect = ^TGraphRect;
  67.   TGraphRect = object(TGraphObject)
  68.     Width, Height: Integer;
  69.     constructor Init;
  70.     procedure Draw; virtual;
  71.     procedure Store(var S: TStream); virtual;
  72.   end;
  73.  
  74. { TGraphObject }
  75. constructor TGraphObject.Init;
  76. begin
  77.   X := Random(GetMaxX);
  78.   Y := Random(GetMaxY);
  79. end;
  80.  
  81. procedure TGraphObject.Draw;
  82. begin
  83.   Abstract;     { Give error: This object should never be drawn }
  84. end;
  85.  
  86. procedure TGraphObject.Store(var S: TStream);
  87. begin
  88.   S.Write(X, SizeOf(X));
  89.   S.Write(Y, SizeOf(Y));
  90. end;
  91.  
  92. { TGraphPoint }
  93. procedure TGraphPoint.Draw;
  94. var
  95.   DX, DY: Integer;
  96. begin
  97.   { Make it a fat point so you can see it }
  98.   for DX := x - 2 to x + 2 do
  99.     for DY := y - 2 to y + 2 do
  100.       PutPixel(DX, DY, 1);
  101. end;
  102.  
  103. { TGraphCircle }
  104. constructor TGraphCircle.Init;
  105. begin
  106.   inherited Init;
  107.   Radius := 20 + Random(20);
  108. end;
  109.  
  110. procedure TGraphCircle.Draw;
  111. begin
  112.   Circle(X, Y, Radius);
  113. end;
  114.  
  115. procedure TGraphCircle.Store(var S: TStream);
  116. begin
  117.   inherited Store(S);
  118.   S.Write(Radius, SizeOf(Radius));
  119. end;
  120.  
  121. { TGraphRect }
  122. constructor TGraphRect.Init;
  123. begin
  124.   inherited Init;
  125.   Width := 10 + Random(20) + X;
  126.   Height := 6 + Random(15) + Y;
  127. end;
  128.  
  129. procedure TGraphRect.Draw;
  130. begin
  131.   Rectangle(X, Y, X + Width, Y + Height);
  132. end;
  133.  
  134. procedure TGraphRect.Store(var S: TStream);
  135. begin
  136.   inherited Store(S);
  137.   S.Write(Width, SizeOf(Width));
  138.   S.Write(Height, SizeOf(Height));
  139. end;
  140.  
  141. { ********************************** }
  142. { **  Stream Registration Records ** }
  143. { ********************************** }
  144.  
  145. const
  146.   RGraphPoint: TStreamRec = (
  147.     ObjType: 150;
  148.     VmtLink: Ofs(TypeOf(TGraphPoint)^);
  149.     Load: nil;                             { No load method yet }
  150.     Store: @TGraphPoint.Store);
  151.  
  152.   RGraphCircle: TStreamRec = (
  153.     ObjType: 151;
  154.     VmtLink: Ofs(TypeOf(TGraphCircle)^);
  155.     Load: nil;                             { No load method yet }
  156.     Store: @TGraphCircle.Store);
  157.  
  158.   RGraphRect: TStreamRec = (
  159.     ObjType: 152;
  160.     VmtLink: Ofs(TypeOf(TGraphRect)^);
  161.     Load: nil;                             { No load method yet }
  162.     Store: @TGraphRect.Store);
  163.  
  164.  
  165. { ********************************** }
  166. { ************  Globals ************ }
  167. { ********************************** }
  168.  
  169. { Abort the program and give a message }
  170.  
  171. procedure Abort(Msg: String);
  172. begin
  173.   Writeln;
  174.   Writeln(Msg);
  175.   Writeln('Program aborting');
  176.   Halt(1);
  177. end;
  178.  
  179. { Register all object types that will be put onto the stream.
  180.   This includes standard TVision types, like TCollection.
  181. }
  182.  
  183. procedure StreamRegistration;
  184. begin
  185.   RegisterType(RCollection);
  186.   RegisterType(RGraphPoint);
  187.   RegisterType(RGraphCircle);
  188.   RegisterType(RGraphRect);
  189. end;
  190.  
  191. { Put the system into graphics mode }
  192.  
  193. procedure StartGraphics;
  194. var
  195.   Driver, Mode: Integer;
  196. begin
  197.   Driver := Detect;
  198.   InitGraph(Driver, Mode, PathToDrivers);
  199.   if GraphResult <> GrOK then
  200.   begin
  201.     Writeln(GraphErrorMsg(Driver));
  202.     if Driver = grFileNotFound then
  203.     begin
  204.       Writeln('in ', PathToDrivers,
  205.         '. Modify this program''s "PathToDrivers"');
  206.       Writeln('constant to specify the actual location of this file.');
  207.       Writeln;
  208.     end;
  209.     Writeln('Press Enter...');
  210.     Readln;
  211.     Halt(1);
  212.   end;
  213. end;
  214.  
  215. { Use the ForEach iterator to traverse and
  216.   show all the collection of graphical objects.
  217. }
  218.  
  219. procedure DrawAll(C: PCollection);
  220.  
  221. { Nested, far procedure. Receives one
  222.   collection element--a GraphObject, and
  223.   calls that elements Draw method.
  224. }
  225.  
  226. procedure CallDraw(P: PGraphObject); far;
  227. begin
  228.   P^.Draw;                            { Call Draw method }
  229. end;
  230.  
  231. begin { DrawAll }
  232.   C^.ForEach(@CallDraw);              { Draw each object }
  233. end;
  234.  
  235. { Instantiate and draw a collection of objects }
  236.  
  237. procedure MakeCollection(var List: PCollection);
  238. var
  239.   I: Integer;
  240.   P: PGraphObject;
  241. begin
  242.   { Initialize collection to hold 10 elements first, then grow by 5's }
  243.   List := New(PCollection, Init(10, 5));
  244.  
  245.   for I := 1 to 12 do
  246.   begin
  247.     case I mod 3 of                      { Create it }
  248.       0: P := New(PGraphPoint, Init);
  249.       1: P := New(PGraphCircle, Init);
  250.       2: P := New(PGraphRect, Init);
  251.     end;
  252.     List^.Insert(P);                     { Add it to collection }
  253.   end;
  254. end;
  255.  
  256. { ********************************** }
  257. { **********  Main Program ********* }
  258. { ********************************** }
  259.  
  260. var
  261.   GraphicsList: PCollection;
  262.   GraphicsStream: TBufStream;
  263. begin
  264.   StreamRegistration;                   { Register all streams }
  265.   StartGraphics;                        { Activate graphics }
  266.  
  267.   { Make the collection and display it }
  268.   MakeCollection(GraphicsList);         { Generate and collect figures }
  269.   DrawAll(GraphicsList);                { Use iterator to draw all }
  270.   Readln;                               { Pause to view figures }
  271.  
  272.   { Put the collection in a stream on disk }
  273.   GraphicsStream.Init('GRAPHICS.STM', stCreate, 1024);
  274.   GraphicsStream.Put(GraphicsList);     { Output collection }
  275.   GraphicsStream.Done;                  { Shut down stream }
  276.  
  277.   { Clean up }
  278.   Dispose(GraphicsList, Done);          { Delete collection }
  279.   CloseGraph;                           { Shut down graphics }
  280. end.
  281.